package com.chenzouquan.service;

import com.chenzouquan.util.*;
import com.chenzouquan.util.format.*;
import com.chenzouquan.util.toJson.CsvToJsonConverter;
import com.chenzouquan.util.toJson.XmlToJsonConverter;
import com.chenzouquan.util.toJson.YamlToJsonConverter;
import com.chenzouquan.util.verify.CSVValidator;
import com.chenzouquan.util.verify.JsonSyntaxValidator;
import com.chenzouquan.util.verify.XMLValidator;
import com.chenzouquan.util.verify.YAMLValidator;

import java.io.File;
import java.io.IOException;

/**
 * 文件转换业务类
 */
public class FileChangeService {

    FileUtil fileUtil = new FileUtil();

    /**
     * 文件转换
     */
    public String FileChange(String filePath,String format){
        // 加载文件
        File file = new File(filePath);
        // 上传文件
        try {
            String path = fileUtil.uploadFile(file);
            // 装换为JSON文件
            if (!path.contains(".json")){
                if (path.contains(".csv")){
                    if (!CSVValidator.validateCSVFormat(path)){
                        return null;
                    }
                    path = CsvToJsonConverter.change(path);
                    JsonFileProcessor.removeQuotesFromJsonFile(path);
                }else if (path.contains(".xml")){
                    if (!XMLValidator.validateXMLSyntax(path)){
                        return null;
                    }
                    path = XmlToJsonConverter.convertXmlToJson(path,path.replace(".xml",".json"));
                }else if (path.contains(".yaml")){
                    if (!YAMLValidator.validateYAML(path)){
                        return null;
                    }
                    path = YamlToJsonConverter.convertYamlToJson(path);
                }
            }else {
                boolean b = JsonSyntaxValidator.validateJsonSyntax(path);
                if (!b){
                    return null;
                }
            }
            // 装换格式
            switch (format){
                case "json":
                    JsonFormatter.formatJsonFile(path);
                    return path;
                case "xml":
                    boolean change = new JsonToXmlConverter().change(path, path.replace(".json", ".xml"));
                    if (change){
                        XmlFormatter.formatXmlFile(path.replace(".json", ".xml"));
                        return path.replace(".json", ".xml");
                    }else {
                        break;
                    }
                case "yaml":
                    boolean change1 = new JsonToYamlConverter().change(path, path.replace(".json", ".yaml"));
                    if (change1){
                        YamlFormatter.formatYamlFile(path.replace(".json", ".yaml"));
                        return path.replace(".json", ".yaml");
                    }else {
                        break;
                    }
                case "csv":
                    boolean change2 = new JsonToCsvConverter().change(path, path.replace(".json", ".csv"));
                    if (change2){
                        CsvFormatter.formatCsvFile(path.replace(".json", ".csv"));
                        return path.replace(".json", ".csv");
                    }else {
                        break;
                    }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
